<?php

class UserController extends Controller
{
	public function actions()
	{
		return array(
			// captcha action renders the CAPTCHA image displayed on the contact page
			'captcha'=>array(
				'class'=>'CCaptchaAction',
				'backColor'=>0xE0E0E0,
				'height'=> 35,
			),
			// page action renders "static" pages stored under 'protected/views/site/pages'
			// They can be accessed via: index.php?r=site/page&view=FileName
			'page'=>array(
				'class'=>'CViewAction',
			),
		);
	}
	
	public function accessRules()
	{
		return array(
			array('allow',  // allow all users to perform 'index' and 'view' actions
				'actions'=>array('register', 'autocomplete', 'logout'),
				'users'=>array('*'),
			),
			array('allow', // allow authenticated user to perform 'create' and 'update' actions
				'actions'=>array('profile'),
				'users'=>array('@'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('admin','delete','index','view'),
				'users'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
		);
	}
	
	public function actionAutocomplete() {
		$criteria = new CDbCriteria;
		$criteria->select = array('id', 'tenquocgia');
	
		$criteria->addSearchCondition('tenquocgia',  strtoupper( $_POST['term']) ) ;
		$criteria->limit = 15;
		$data = Quocgia::model()->findAll($criteria);
	
		$arr = array();
	
		foreach ($data as $item) {
			 
			$arr[] = array(
					'id'=>$item->tenquocgia,
					'value' => $item->tenquocgia,
					
			);
		}
	
		echo CJSON::encode($arr);
	
	}
	
	public function actionIndex()
	{
		$this->render('index');
	}

	
	public function actionRegister(){
		$model = new User;
		if(isset($_POST['User'])){
			$model->attributes = $_POST['User'];
			if(Userdao::Register($model)){
				$this->redirect(array('site/index'));
			}
		}
		$var =Quocgia::model()->findAll();
		$data['quocgia'] = CHtml::listData($var, "tenquocgia", "tenquocgia");
		$data['model']=$model;
		$this->render('register', $data);
	}
	
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
}